//
//  NewFeafureViewController.swift
//  01_新浪微博
//
//  Created by 黄蓉 on 15/12/16.
//  Copyright © 2015年 黄蓉. All rights reserved.
//

import UIKit
import SnapKit

private let imageCount = 4

class NewFeafureViewController: UICollectionViewController {

    init() {
        let layout = UICollectionViewFlowLayout()
        super.init(collectionViewLayout: layout)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        
       self.collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: "Cell")
    }
    
    
    //MARK: 数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! NewFeatureCell
        
        cell.index = indexPath.item
   
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        let cell = collectionView.visibleCells().last as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)
        
        if  index?.item == imageCount - 1 {
            cell.startAnimations()
        }
            
    }
    
    
}




//MARK: cell
class NewFeatureCell : UICollectionViewCell {
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置cell的页面布局
    private func setupUI() {
        
        contentView.addSubview(iconView)
        contentView.addSubview(button)
    
        //设置背景图片的约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
        //设置按钮的位置约束
        button.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        
     }
     
    var index: Int = 0 {
        didSet {
            //按钮 的隐藏必须这里写,不然cell重用的时候,会讲按钮也重用过来
            button.hidden = true
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
        }
    }
    
    
    ///新页面的背景图
    lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    ///进入按钮
    lazy var button : UIButton = UIButton(title: "开始体验", normalImageName: "new_feature_finish_button", color:UIColor.whiteColor(),target:self, action: "startButtonClick")
    
    
    ///监听按钮的点击事件
    @objc private func startButtonClick() {
        print(__FUNCTION__)
        
        
    }
    ///开始按钮的动画方法
    func startAnimations() {
        
        button.transform = CGAffineTransformMakeScale(0, 0)
        button.hidden = false
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.button.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
            NSNotificationCenter.defaultCenter().postNotificationName(notification, object: nil)
        }
    }
    
}













